package com.woniu.cms.auth.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.woniu.cms.auth.core.entity.Company;
import com.woniu.cms.auth.core.entity.Position;
import com.woniu.cms.auth.core.mapper.PositionMapper;
import com.woniu.cms.auth.core.service.ICompanyService;
import com.woniu.cms.auth.core.service.IPositionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.cms.auth.core.vo.PositionVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lilili
 * @since 2022-09-16
 */
@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements IPositionService {
    @Resource
    private PositionMapper positionMapper;
    @Resource
    private ICompanyService companyService;
    @Transactional
    @Override
    public List<PositionVo> getAll() {
        return positionMapper.getAll();
    }

    /**
     * 添加公司名称返回自增主键
     * 把职位名称和主键一起加入职位表
     *
     * @param positionName 职位名称
     * @param companyName  公司名称
     * @return {@link Boolean}
     */
    @Transactional
    @Override
    public Boolean commit(String positionName, String companyName) {
        Company one = companyService.getOne(new LambdaQueryWrapper<Company>().eq(Company::getCompanyName, companyName));
        if(one != null){
            int insert = positionMapper.insert(new Position().setPositionName(positionName).setCompanyId(one.getId()));
            if(insert == 0){
                throw new RuntimeException("职位添加失败");
            }
        }else{
            Company company = new Company().setCompanyName(companyName);
            boolean save = companyService.save(company);
            if(!save){
                throw new RuntimeException("公司名称添加失败");
            }
            int insert = positionMapper.insert(new Position().setPositionName(positionName).setCompanyId(company.getId()));
            if(insert == 0){
                throw new RuntimeException("职位添加失败");
            }
        }


        return true;
    }

    @Transactional
    @Override
    public boolean updatePosition(PositionVo positionVo) {
        Position position = positionMapper.selectById(positionVo.getId());
        position.setPositionName(positionVo.getPositionName());
        //判断公司名有没有改动
        Company one = companyService.getById(position.getCompanyId());
        if(!positionVo.getCompanyName().equals(one.getCompanyName())){
            companyService.updateById(
                    new Company().setCompanyName(positionVo.getCompanyName()).setId(position.getCompanyId())
            );
        }

        int i = positionMapper.updateById(position);
        if(i == 0){
            throw new RuntimeException("职位更新失败");
        }
        return true;
    }

    @Transactional
    @Override
    public boolean removePosition(Integer id) {
        int i = positionMapper.deleteById(id);
        if(i == 0){
            throw new RuntimeException("职位删除失败");
        }
        return true;
    }

    @Transactional
    @Override
    public List<String> getUserByPositionName(String name) {
        List<String> names = positionMapper.getUserByPositionName(name);
        return names;
    }

    @Transactional
    @Override
    public List<Integer> getUserIdByPositionName(String name) {
        List<Integer> ids = positionMapper.getUserIdByPositionName(name);
        return ids;
    }
}
